package com.bang.study.day02.test;

import com.bang.study.day02.FileOperation;
import com.bang.study.day02.map.BSTMap;
import com.bang.study.day02.map.LinkedListMap;
import com.bang.study.day02.map.Map;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.TreeMap;

/**
 * @Auther: Bang
 * @Date: 2019/9/14 20:36
 * @Description: 测试不同底层实现map的效率
 */
public class MapMain {
    private static double testMap(Map<String, Integer> map, String filename){

        long startTime = System.nanoTime();

        System.out.println(filename);
        ArrayList<String> words = new ArrayList<>();
        if(FileOperation.readFile(filename, words)) {
            System.out.println("Total words: " + words.size());

            for (String word : words){
                if(map.contains(word)) {
                    map.set(word, map.get(word) + 1);
                } else {
                    map.add(word, 1);
                }
            }

            System.out.println("Total different words: " + map.getSize());
            System.out.println("Frequency of PRIDE: " + map.get("pride"));
            System.out.println("Frequency of PREJUDICE: " + map.get("prejudice"));
        }

        long endTime = System.nanoTime();

        return (endTime - startTime) / 1000000000.0;
    }

    public static void main(String[] args) {

        String filename = "pride-and-prejudice.txt";

        BSTMap<String, Integer> bstMap = new BSTMap<>();
        double time1 = testMap(bstMap, filename);
        System.out.println("BST Map: " + time1 + " s");

        System.out.println();

        LinkedListMap<String, Integer> linkedListMap = new LinkedListMap<>();
        double time2 = testMap(linkedListMap, filename);
        System.out.println("Linked List Map: " + time2 + " s");


        System.out.println();

        HashMap<String, Integer> hashMap = new HashMap<>();
        double time3 = hashMap(hashMap, filename);
        System.out.println("HashMap: " + time3 + " s");


        System.out.println();

        TreeMap<String, Integer> treeMap = new TreeMap<>();
        double time4 = hashMap(treeMap, filename);
        System.out.println("TreeMap: " + time4 + " s");


        System.out.println();

        LinkedHashMap<String, Integer> linkedHashMap = new LinkedHashMap<>();
        double time5 = hashMap(linkedHashMap, filename);
        System.out.println("LinkedHashMap: " + time5 + " s");

    }

    private static double hashMap(java.util.Map map, String filename){

        long startTime = System.nanoTime();

        System.out.println(filename);
        ArrayList<String> words = new ArrayList<>();
        if(FileOperation.readFile(filename, words)) {
            System.out.println("Total words: " + words.size());

            for (String word : words){
                if(map.containsKey(word)) {
                    Integer o = (Integer) map.get(word);
                    map.put(word, o + 1);
                }
                else {
                    map.put(word, 1);
                }
            }

            System.out.println("Total different words: " + map.size());
            System.out.println("Frequency of PRIDE: " + map.get("pride"));
            System.out.println("Frequency of PREJUDICE: " + map.get("prejudice"));
        }

        long endTime = System.nanoTime();

        return (endTime - startTime) / 1000000000.0;
    }
}
